//
//  MCMineHeaderView.m
//  TLYL
//
//  Created by MC on 2017/6/12.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMineHeaderView.h"
#import "NSString+Helper.h"

#define WIDTH_BTN  45
@interface MCMineHeaderView()

@property (nonatomic ,strong)UIImageView * backImgV;

/*
 * 打底一
 */
@property (nonatomic,strong)UIView * upView;

//用户头像
@property (nonatomic,strong)UIImageView * test_userIconImgV;
@property (nonatomic,strong)UIImageView * userIconImgV;

//用户名
@property (nonatomic,strong)UIButton * userNameBtn;


//充值
@property (nonatomic,strong)UIButton * rechargeBtn;
//提款
@property (nonatomic,strong)UIButton * withdrawBtn;
//站内信
@property (nonatomic,strong)UIButton * messageBtn;


/*
 * 打底二
 */
@property (nonatomic,strong)UIView * downView;



@end

@implementation MCMineHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    _backImgV=[[UIImageView alloc]init];
    [self addSubview:_backImgV];
    _backImgV.frame=CGRectMake(0, 0, G_SCREENWIDTH, 200);
    _backImgV.image=[UIImage imageNamed:@"BackHeaderView"];
    
    self.backgroundColor=[UIColor clearColor];
    
    /*
     * 上半部分视图
     */
    _upView=[[UIView alloc]init];
    [self addSubview:_upView];
    _upView.backgroundColor=[UIColor clearColor];
    _upView.frame=CGRectMake(0, 20, G_SCREENWIDTH, 147);
    
    /*
     * 打底二
     */
    _downView=[[UIView alloc]init];
    [self addSubview:_downView];
    _downView.backgroundColor=[UIColor clearColor];
    _downView.frame=CGRectMake(0, 147+20, G_SCREENWIDTH, 33);
    
    /**
     * 设置按钮
     **/
    _settingImgV=[[UIImageView alloc]init];
    [_upView addSubview:_settingImgV];
    _settingImgV.userInteractionEnabled=YES;
    _settingImgV.image=[UIImage imageNamed:@"personal-set-up"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_setting)];
    [_settingImgV addGestureRecognizer:tap];
    
    /*
     * 用户头像
     */
    _userIconImgV=[[UIImageView alloc]init];
    [_upView addSubview:_userIconImgV];
    _userIconImgV.userInteractionEnabled=YES;
    _userIconImgV.image=[UIImage imageNamed:@"MoRenPerson"];
    _userIconImgV.frame=CGRectMake(23, 35, 75, 75);
    
    _test_userIconImgV=[[UIImageView alloc]init];
    [_upView addSubview:_test_userIconImgV];
    _test_userIconImgV.userInteractionEnabled=YES;
    _test_userIconImgV.image=[UIImage imageNamed:@"testMoRenPerson"];
    _test_userIconImgV.frame=CGRectMake(23, 35, 75, 75);
    
    /*
     * 用户名
     */
    _userNameBtn=[[UIButton alloc]init];
    _userNameBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_userNameBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_upView addSubview:_userNameBtn];
    [_userNameBtn setImage:[UIImage imageNamed:@"personal-xiu-gai"] forState:UIControlStateNormal];
    [_userNameBtn setTitle:@"加载中..." forState:UIControlStateNormal];
    _userNameBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    _userNameBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    _userNameBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [_userNameBtn addTarget:self action:@selector(personalXiuGaiClick:) forControlEvents:UIControlEventTouchUpInside];

    [_userNameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_userIconImgV.mas_centerX);
        make.top.equalTo(_userIconImgV.mas_bottom).offset(7);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(121);
    }];
    //充值
    _rechargeBtn=[[UIButton alloc]init];
    [self setBtn:_rechargeBtn andTitle:@"充值" andImgName:@"personal-finance-recharget" andIndex:1];
    //提款
    _withdrawBtn=[[UIButton alloc]init];
    [self setBtn:_withdrawBtn andTitle:@"提款" andImgName:@"personal-finance-drawingt" andIndex:0];
    //站内信
    _messageBtn=[[UIButton alloc]init];
    [self setBtn:_messageBtn andTitle:@"站内信" andImgName:@"personal-letter-letter" andIndex:2];
 
/******************************************/
    

    /*
     * 可用余额
     */
    _lab_accountBalance=[[UILabel alloc]init];
    _lab_accountBalance.font = [UIFont systemFontOfSize:12];
    _lab_accountBalance.numberOfLines=1;
    _lab_accountBalance.textColor=[UIColor whiteColor];
    _lab_accountBalance.text = @"可用余额";
    _lab_accountBalance.textAlignment=NSTextAlignmentCenter;
    [_downView addSubview:_lab_accountBalance];
    _lab_accountBalance.frame=CGRectMake(0, 0, G_SCREENWIDTH/2.0, 33);
    
    
    
    /*
     * 冻结金额
     */
    _lab_frozenAccount=[[UILabel alloc]init];
    _lab_frozenAccount.font = [UIFont systemFontOfSize:12];
    _lab_frozenAccount.numberOfLines=1;
    _lab_frozenAccount.text = @"冻结余额";
    _lab_frozenAccount.textColor=[UIColor whiteColor];
    _lab_frozenAccount.textAlignment=NSTextAlignmentCenter;
    [_downView addSubview:_lab_frozenAccount];

    _lab_frozenAccount.frame=CGRectMake(G_SCREENWIDTH/2.0, 0, G_SCREENWIDTH/2.0, 33);
    
    
    
    
    [_rechargeBtn addTarget:self action:@selector(action_recharge) forControlEvents:UIControlEventTouchUpInside];
    [_withdrawBtn addTarget:self action:@selector(action_withdraw) forControlEvents:UIControlEventTouchUpInside];
    [_messageBtn addTarget:self action:@selector(action_message) forControlEvents:UIControlEventTouchUpInside];
    
    [self layOutConstraints];
    
}

-(void)setBtn:(UIButton *)btn andTitle:(NSString *)title andImgName:(NSString *)imgName andIndex:(int)index{
    //小球宽度

    CGFloat SPadding=(G_SCREENWIDTH-23-75-3*WIDTH_BTN)/35.0;
    
    CGFloat Padding=SPadding*8;//10+8+8+9
    
    CGFloat WIDTH_L=23+75+SPadding*10;
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [btn setTitle:title forState:UIControlStateNormal];
    [_upView addSubview:btn];
    btn.imageView.layer.cornerRadius=35/2.0;
    btn.imageView.clipsToBounds=YES;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(WIDTH_BTN);
        make.left.equalTo(_upView.mas_left).offset(WIDTH_L+WIDTH_BTN*index+Padding*index);
        make.height.mas_equalTo(WIDTH_BTN+20);
        make.centerY.equalTo(_userIconImgV.mas_centerY);
    }];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize: 12];
    [self setButtonContentCenter:btn];
}

-(void)layOutConstraints{

    /**
     * 设置按钮
     **/
    [_settingImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_upView.mas_top).offset(10);
        make.right.equalTo(_upView.mas_right).offset(-10);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(25);
    }];

    
}

/**
 *   获取字符宽度
 */
- (CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
}
-(void)setDataSource:(MCMineInfoModel*)dataSource{
    _dataSource=dataSource;

//    _lab_accountBalance.text = @"可用余额:3000000元";
//    _lab_frozenAccount.text = @"冻结余额:3000000元";
//    NSString * UserName=[NSString stringWithFormat:@"%@",dataSource.UserRealName];
    NSString * UserName=[[NSUserDefaults standardUserDefaults] stringForKey:@"UserName"];
    
    CGFloat L=[NSString getWidthWithTitle:UserName font:MC_REALVALUE(12)]/2.0+60.5;
    CGFloat title_L=-15;
    _userNameBtn.titleEdgeInsets = UIEdgeInsetsMake(0,title_L, 0, 0);
    [_userNameBtn setTitle:UserName forState:UIControlStateNormal];
    _userNameBtn.imageEdgeInsets = UIEdgeInsetsMake(0,L, 0, 0);
    if ([dataSource.UserType intValue]==1) {
        _test_userIconImgV.hidden=NO;
    }else{
        _test_userIconImgV.hidden=YES;
    }
}

#pragma mark-修改用户名
-(void)personalXiuGaiClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(goToModifyUserNameViewController)]) {
        [self.delegate goToModifyUserNameViewController];
    }
}


#pragma mark-跳转设置
-(void)action_setting{
    if ([self.delegate respondsToSelector:@selector(goToSettingViewController)]) {
        [self.delegate goToSettingViewController];
    }
}

#pragma mark- 跳转充值
-(void)action_recharge{
    if ([self.delegate respondsToSelector:@selector(goToRechargeViewController)]) {
        [self.delegate goToRechargeViewController];
    }
}


#pragma mark-跳转提现
-(void)action_withdraw{
    
    if ([self.delegate respondsToSelector:@selector(goToWithdrawViewController)]) {
        [self.delegate goToWithdrawViewController];
    }
}

#pragma mark-跳转站内信
-(void)action_message{
    if ([self.delegate respondsToSelector:@selector(goToMessageViewController)]) {
        [self.delegate goToMessageViewController];
    }
}


-(void)setButtonContentCenter:(UIButton *)button
{
    CGSize imgViewSize,titleSize,btnSize;
    UIEdgeInsets imageViewEdge,titleEdge;
    

    CGFloat imgV_W=35;
    //设置按钮内边距
    imgViewSize = button.imageView.bounds.size;
    titleSize = button.titleLabel.bounds.size;
    btnSize = button.bounds.size;
    CGFloat L_Space=(btnSize.width-imgV_W)/2.0;
    CGFloat R_Space=L_Space;
    CGFloat H_Space = (btnSize.height/2.0-75/2.0)+75/2.0-imgV_W/2.0;
    CGFloat bottomSpace = btnSize.height-H_Space-imgV_W;
    

    imageViewEdge = UIEdgeInsetsMake(H_Space,L_Space, bottomSpace, R_Space);
    [button setImageEdgeInsets:imageViewEdge];
    titleEdge = UIEdgeInsetsMake(H_Space+imgV_W+5, - imgViewSize.width-5, 0.0, 0.0);
    [button setTitleEdgeInsets:titleEdge];
}
@end













































